<template>
  <n-space vertical>
    <n-flex>
      <n-date-picker :disabled="commonStore.loading" v-model:value="range" type="daterange" clearable class="w-280px"/>
      <n-button :loading="commonStore.loading" :disabled="commonStore.loading" type="primary" tertiary
                @click="computePreferredStock">
        生成优选股票
      </n-button>
    </n-flex>
    <n-log ref="logInstRef" font-size="12" line-height="1.5" :log="logs"
           style="border: 1px solid lightgray;border-radius: 5px;padding-left: 10px;padding-top: 10px;padding-bottom: 10px"/>
  </n-space>
</template>

<script setup lang="ts">
import {LogInst, NButton} from 'naive-ui'
import {MarketService} from "../../bindings/quant/backend/service";
import {nextTick, onMounted, ref, watchEffect} from "vue";
import dayjs from 'dayjs';
import {Events} from "@wailsio/runtime";
import {useCommonStore} from "../store/common";

const commonStore = useCommonStore()
const logs = ref("")
// const logInstRef = ref(null);
const logInstRef = ref<LogInst | null>(null)
// 今天
const range = ref([dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')])

const computePreferredStock = () => {
  let startDate = dayjs(range.value[0]).format('YYYYMMDD')
  let endDate = dayjs(range.value[1]).format('YYYYMMDD')
  console.log(startDate)
  console.log(endDate)
  commonStore.loading = true
  MarketService.CalculatePreferred(startDate, endDate).then((result) => {
    console.log(result)
  }).catch((err) => {
    console.log("生成优选股票出现错误：" + err);
  }).finally(() => {
    commonStore.loading = false
  });
}


onMounted(() => {
  watchEffect(() => {
    if (logs.value) {
      nextTick(() => {
        logInstRef.value?.scrollTo({position: "bottom", silent: true});
      });
    }
  });

  Events.On('log', (obj) => {
    logs.value += obj.data + "\n";
  });
})
</script>


<style scoped>

</style>